package com.exartisan.yn_event;

import java.util.ArrayList;

import android.app.ProgressDialog;
import android.content.Intent;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.GridView;

import com.exartisan.adapter.LazyAdapter;
import com.exartisan.adapter.VideoAdapter;
import com.exartisan.bean.GalleryContent;
import com.exartisan.network.NetworkUtility;
import com.exartisan.network.RestConnector;
import com.exartisan.network.StupidResponseTranslater;
import com.exartisan.network.WeirdParametersBuilder;
import com.exartisan.ynevent.BaseActivity;
import com.loopj.android.http.AsyncHttpResponseHandler;

public class Gallery extends BaseActivity
{

	GridView list_img, list_video;
	ArrayList<GalleryContent> galleryContents;
	ProgressDialog progressDialog;
	

	
	@Override
	protected void initUI()
	{
		setContentView(R.layout.gallery_activity);

		list_img = (GridView) findViewById(R.id.gv_img);
		list_video = (GridView) findViewById(R.id.gv_video);
	}

	@Override
	protected void initProcess()
	{
		if (galleryContents == null) doNetWorkTask();
		else 
			setUpGridViews();
	}
	
	private void doNetWorkTask(){
		if (NetworkUtility.checkNetworkState(getApplicationContext())){
			progressDialog = ProgressDialog.show(this, "Tải dữ liệu", "Xin chờ ...");
			String event_id = getIntent().getStringExtra("eventId");
			RestConnector.post("", WeirdParametersBuilder.BuildParams(WeirdParametersBuilder.BuildEventGalleryData(event_id)), new AsyncHttpResponseHandler(){
				@Override
				public void onSuccess(int arg0, String response) {
					progressDialog.dismiss();
					if (StupidResponseTranslater.checkSuccess(response)){
						galleryContents = StupidResponseTranslater.getEventGallery(response);
						setUpGridViews();
					}
					else {
						Toast.makeText(getApplicationContext(), StupidResponseTranslater.getErrorMessage(response), Toast.LENGTH_LONG).show();
						finish();
					}
					super.onSuccess(arg0, response);
				}
				
				@Override
				public void onFailure(Throwable arg0, String arg1) {
					Toast.makeText(getApplicationContext(), "Không thể kết nối", Toast.LENGTH_LONG).show();
					finish();
					super.onFailure(arg0, arg1);
				}
				
			});
		}
		else {
			Toast.makeText(this, "Không có kết nối mạng", Toast.LENGTH_LONG).show();
			finish();
		}
	}
	
	private void setUpGridViews(){
		ArrayList<String> imgUrls = new ArrayList<String>();
		ArrayList<String> videoUrls = new ArrayList<String>();
		for (GalleryContent c  : galleryContents) {
			if (c.getType() == 1) imgUrls.add(c.getResourceUrl());
			else
				videoUrls.add(c.getResourceUrl());
		}
		list_img.setAdapter(new LazyAdapter(getBaseContext(), imgUrls));
		list_video.setAdapter(new VideoAdapter(getBaseContext(), videoUrls));
		
		list_img.setOnItemClickListener(new OnItemClickListener()
		{

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int pos,
					long id)
			{
				Intent i = new Intent(getBaseContext(), PhotoActivity.class);
				i.putExtra("pos", pos);
				i.putExtra("data", galleryContents);
				startActivity(i);
			}
		});
		list_video.setOnItemClickListener(new OnItemClickListener()
		{

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3)
			{
				Intent i = new Intent(getBaseContext(), VideoActivity.class);
				i.putExtra("data", galleryContents);
				startActivity(i);
			}
		});
	}

	@Override
	public void onDestroy()
	{
		list_img.setAdapter(null);
		super.onDestroy();
	}

}
